package com.hup.restTimer.view;

import com.hup.restTimer.service.MainService;
import com.hup.restTimer.view.setting.SettingFrame;
import com.hup.utils.commons.start.AppIcon;
import com.hup.utils.commons.start.BaseAppTray;
import com.hup.utils.commons.start.BaseAppTray.TrayActionListener;
import com.hup.utils.commons.start.SpringContext;
import com.hup.utils.commons.start.StartupInfo;
import com.hup.utils.commons.start.VersionConfig;
import lombok.extern.slf4j.Slf4j;

import javax.swing.JCheckBoxMenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author hugan
 * @date 2021/9/14
 */
@Slf4j
public class AppTray extends BaseAppTray implements ActionListener, TrayActionListener {

    private static final String CMD_NOT_TIPS = "关闭久坐提醒";
    private static final String CMD_REST = "开始休息";
    private static final String CMD_SETTING = "设置";

    public AppTray() {
        super(SpringContext.getBean(AppIcon.class).getImage(), StartupInfo.getApplicationChName());
        setPopupMenu(new SimpleMenu()
                .addHeader(SpringContext.getBean(VersionConfig.class).version)
                .addSeparator0()
                .addMenu(new JCheckBoxMenuItem(CMD_NOT_TIPS))
                .addSeparator0()
                .addMenu(CMD_REST)
                .addMenu(CMD_SETTING)
                .addSeparator0()
                .addExit(), this);
        setTrayListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        switch (e.getActionCommand()) {
            case CMD_NOT_TIPS:
                SpringContext.getBean(MainService.class).setNotTips(((JCheckBoxMenuItem) e.getSource()).isSelected());
                break;
            case CMD_REST:
                SpringContext.getBean(MainService.class).switchRest(true);
                break;
            case CMD_SETTING:
                SettingFrame.switchShow(true);
                break;
        }
    }

    @Override
    public void onClickTray() {
        SettingFrame.switchShow(null);
    }

    @Override
    public void onExit(BaseAppTray appTray) {
        System.exit(0);
    }

}
